<template>
	<skeleton :skeleton="{name:'simulation_zf'}" :showSkeleton="showSkeleton">
		<block slot="content">
			<noData :showErrorTip="noData">
				<view class="panel" slot="content">
					<view class="panel-body" v-for="(item, index) in metaData" :key="index">
						<view class="panel-body-item">
							<view class="item-info">
								<view class="item-info__h3">{{ item.title }}</view>
								<view class="item-info__h6">共{{item.sujectNum}}题，总时间{{item.examMin}}分钟</view>
							</view>
							<view class="item-timeBox">
								<text v-if="item.recordId==undefined||item.recordId==''" class="item-time" @click="linkTopic(index,item.examId,item.recordId)">开始考试</text>
								<text v-else class="item-time" @click="analysis(item.examId,item.recordId)">查看解析</text>
							</view>
						</view>
					</view>
				</view>
				<view class="loading" v-if="textShow">没有更多了</view>
			</noData>
		</block>
	</skeleton>
</template>

<script>
	import {isEmpty} from '@/utils/index';
	import ExamService from "@/services/exam/exam.js";
	const service = new ExamService();
	export default {
		data() {
			return {
				examType:8,
				textShow: false,
				pageNum: 1,
				pageSize: 20,
				metaData: [],
				ascription: undefined,
				showSkeleton: true,
				noData: false,
				curIndex:-1
			};
		},
		onLoad(query) {
			uni.$on('examFinish',(payload)=>{
				if (this.curIndex < 0 || isEmpty(this.metaData) || this.metaData.length <= this.curIndex) {
					return;
				}
				let item = this.metaData[this.curIndex];
				if (isEmpty(item)) {
					return;
				}
				item.recordId = payload.recordId;
				this.curIndex = -1;
			});
			this.ascription = query.ascription;
			this.loadData(1);
		},
		onUnload() {
			uni.$off('examFinish');
		},
		onPullDownRefresh() {
			this.loadData(1);
		},
		onReachBottom() {
			this.loadData(this.pageNum+1);
		},
		methods: {
			loadData(pageNum) {
				let param = {
					pageNum: pageNum,
					pageSize: this.pageSize,
					homeType: this.ascription,
					resultType: this.examType,
					examType: 0
				}
				service
					.pastExamPaper(param)
					.then(res => {
						uni.stopPullDownRefresh();
						if (res.success) {
							if (this.pageNum > 1) {
								this.textShow = res.result.list.length < this.pageSize;
								const list = this.handleData( res.result.list);
								this.metaData = [...this.metaData, ...list];
							} else {
								this.textShow = false;
								this.metaData = this.handleData( res.result.list);
								this.noData = this.metaData.length == 0;
							}
							this.pageNum = res.result.pageNum;
						}
					})
					.then(() => {
						this.showSkeleton = false; //页面请求完成，取消骨架屏展示
					});
			},
			handleData(dataList){
				if(isEmpty(dataList)){
					return [];
				}
				dataList.forEach((item,index)=>{
					item.recordId = isEmpty(item.recordId)?'':item.recordId;
				});
				return dataList;
			},
			linkTopic(index,examId, recordId) {
				this.curIndex = index;
				recordId = recordId == undefined ? "" : recordId
				uni.navigateTo({
					url: "/pages/exam/examsub?examType="+this.examType+"&examId=" + examId + "&recordId=" + recordId
				});
			},
			analysis(examId, recordId){
				uni.navigateTo({
					url: '/pages/exam/answerAnalysis?examId=' + examId
					+'&recordId=' + recordId 
					+'&analyType=1'
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {}

	.panel {
		&-body {
			width: 100%;
			background: #fff;
			border-radius: 8upx;
			padding: 10upx 20upx;
			border-bottom: 1upx dashed #dddddd;

			&-item {
				height: 108upx;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				text-align: left;

				.item-info {
					flex: 1;

					&__h3 {
						font-size: 28upx;
						color: #333;
					}

					&__h6 {
						font-size: 24upx;
						color: #999999;
						margin-top: 10upx;
					}
				}

				.item-timeBox {
					text-align: center;

					.item-time {
						width: 140upx;
						height: 50upx;
						background-color: #377cfd;
						border-radius: 6upx;
						color: #fff;
						text-align: center;
						line-height: 50upx;
						font-size: 20upx;
						display: block;
					}

					.item-text {
						font-size: 20upx;
						display: block;
						color: #999999;
						margin-top: 5px;
					}
				}
			}
		}

		&-body:last-child {
			border: 0;
		}
	}

	.loading {
		text-align: center;
		font-size: 28upx;
		padding: 20upx 0;
		color: #999;
	}
	.gray-btn{
		color: #fff;
		background-color: #ededed;
	}
</style>
